package yxy.game.pm2.module.threelinepush;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;

import java.util.ArrayList;
import java.util.HashMap;

//三线互推缓存
public class ThreeLinePushModule extends PlayerModule {
    // bean===========================================================================

    private int currentMapId;//当前关卡
    private long gold;//金币
    private HashMap<Integer,Integer> monster = new HashMap<Integer,Integer>();//我方怪科技等级 key id  value 等级
    private DateTime updateTime ;
    private ArrayList<Integer> rewardIds;//领取的关卡奖励id

    public long getGold() {
        return gold;
    }

    public void setGold(long gold) {
        this.gold = gold;
    }

    public HashMap<Integer, Integer> getMonster() {
        return monster;
    }

    public void setMonster(HashMap<Integer, Integer> monster) {
        this.monster = monster;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    public int getCurrentMapId() {
        return currentMapId;
    }

    public void setCurrentMapId(int currentMapId) {
        this.currentMapId = currentMapId;
    }

    public ArrayList<Integer> getRewardIds() {
        return rewardIds;
    }

    public void setRewardIds(ArrayList<Integer> rewardIds) {
        this.rewardIds = rewardIds;
    }

    // func===========================================================================

    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:THREELINEPUSH", playerCode);
    }

    public void set() {
        try {
            String key1 = key(getPlayerCode());
            setv(key1, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public ThreeLinePushModule get(String playerCode) {
        try {
            String key = key(playerCode);
            ThreeLinePushModule module = getv(key);
            //todo 加到数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key, ThreeLinePushModule.class);
                if (module != null) module.set();
            }

            if (module == null) {
                module = create(playerCode);
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //初始化
    static private ThreeLinePushModule create(String playerCode) {
        ThreeLinePushModule module = new ThreeLinePushModule();
        module.setCurrentMapId(1);
        module.setPlayerCode(playerCode);
        module.setRewardIds(new ArrayList<>());
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= 3; i++) {
            map.put(i,0);
        }
        module.setMonster(map);
        module.setGold(0);
        module.setUpdateTime(DateTime.now().minusDays(1));
        module.set();
        return module;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ThreeLinePushModule threeLinePushModule = ThreeLinePushModule.get(playerCode);
                MySQLCacheDAO.setV(key, threeLinePushModule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }

}
